package com.ksd.modules.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.members.entity.AdminUserWalletIncome;
import com.ksd.modules.members.mapper.AdminUserWalletIncomeMapper;
import com.ksd.modules.members.po.AdminUserWalletIncomeSavePO;
import com.ksd.modules.members.po.AdminUserWalletIncomeSearchPO;
import com.ksd.modules.members.service.IAdminUserWalletIncomeService;
import com.ksd.modules.members.vo.AdminUserWalletIncomeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminUserWalletIncomeServiceImpl extends BaseServiceImpl<AdminUserWalletIncomeMapper, AdminUserWalletIncome> implements IAdminUserWalletIncomeService {


    /**
     * 查询列表
     * @param adminUserWalletSearchPO
     * @return
     */
    public Page<AdminUserWalletIncomeVo> findAdminUserWalletIncomes(AdminUserWalletIncomeSearchPO adminUserWalletSearchPO){
        Page<AdminUserWalletIncome> page = new Page<>(adminUserWalletSearchPO.getPageNo(),adminUserWalletSearchPO.getPageSize());
        LambdaQueryWrapper<AdminUserWalletIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminUserWalletIncome::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminUserWalletSearchPO.getPayMethod()), AdminUserWalletIncome::getPayMethod,adminUserWalletSearchPO.getPayMethod());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserWalletSearchPO.getOrderno()), AdminUserWalletIncome::getOrderno,adminUserWalletSearchPO.getOrderno());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserWalletSearchPO.getUuid()), AdminUserWalletIncome::getUuid,adminUserWalletSearchPO.getUuid());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminUserWalletSearchPO.getPhone()), AdminUserWalletIncome::getPhone,adminUserWalletSearchPO.getPhone());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminUserWalletSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminUserWalletIncome::getTitle,adminUserWalletSearchPO.getKeyword())
                    .or().like(AdminUserWalletIncome::getNickname, adminUserWalletSearchPO.getKeyword())
                    .or().like(AdminUserWalletIncome::getUsername, adminUserWalletSearchPO.getKeyword())

            );
        }

        lambdaQueryWrapper.orderByDesc(AdminUserWalletIncome::getCreateTime);
        Page<AdminUserWalletIncome> adminUserWalletPage = this.page(page, lambdaQueryWrapper);
        Page<AdminUserWalletIncomeVo> adminUserWalletVoPage = tranferPageBo(adminUserWalletPage, AdminUserWalletIncomeVo.class);
        return adminUserWalletVoPage;
    }


    /**
     * 保存和更新
     * @param adminUserWalletSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminUserWalletIncomeVo saveUpdateAdminUserWalletIncome(AdminUserWalletIncomeSavePO adminUserWalletSavePO){
        AdminUserWalletIncome adminUserWallet = tranferBo(adminUserWalletSavePO, AdminUserWalletIncome.class);
        adminUserWallet.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserWallet.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(adminUserWallet);
        AdminUserWalletIncomeVo adminUserWalletVo = tranferBo(adminUserWallet, AdminUserWalletIncomeVo.class);
        return adminUserWalletVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminUserWalletIncomeVo getAdminUserWalletIncomeById(Long id){
        AdminUserWalletIncome adminUserWallet = this.getById(id);
        AdminUserWalletIncomeVo adminUserWalletVo = tranferBo(adminUserWallet, AdminUserWalletIncomeVo.class);
        return adminUserWalletVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminUserWalletIncomeById(Long id){
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminUserWalletIncome(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }

}
